package com.changli03.threadsafe.threadsafe;

public class WindowTest1 {
    public static void main(String[] args) {
        Window1 w1 = new Window1();
        Window1 w2 = new Window1();
        Window1 w3 = new Window1();
        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();
    }
}
class Window1 extends Thread {
    static int ticketnum = 100;
    static  boolean flag = true;
    @Override
    public void run() {
        while (flag) {
            show();
        }
    }
//    public  synchronized void show (){//非静态方法的同步监视器为 this  w1,w2,w3
    public static synchronized void show (){//静态方法的同步监视器为 当前类 即 Window1.class

        if (ticketnum > 0) {
            System.out.println(Thread.currentThread().getName() + "票号为:" + ticketnum);
            ticketnum--;
        }
        else{
            flag = false;
        }
    }
}
